package com.asia5b.a5mall.goods.fragment

import android.os.Handler
import com.google.android.material.tabs.TabLayout
import androidx.recyclerview.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.goods.R
import com.asia5b.a5mall.goods.adapter.GoodsCommentsAdapter
import com.asia5b.a5mall.goods.databinding.FragmentGoodscommentBinding
import com.asia5b.a5mall.middleware.base.SimpleFragment
import com.asia5b.a5mall.middleware.logic.viewmodel.goods.GoodsCommentFragmentVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.goods.Goods2CommenModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.utils.newEmptyView
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.fragment_goodscomment.*

/**
 * @Note   商品评论页面
 */
@Route(path = ActCons.GoodsCommentFragment)
class GoodsCommentFragment : SimpleFragment<GoodsCommentFragmentVM, FragmentGoodscommentBinding>() {

    override val viewModelClazz: Class<GoodsCommentFragmentVM>
        get() = GoodsCommentFragmentVM::class.java

    override fun getDataBinding(container: ViewGroup?): FragmentGoodscommentBinding {
        return FragmentGoodscommentBinding.inflate(layoutInflater, container, false)
    }

    val gradleTextList by lazy {
        arrayListOf<String>(
                getString(R.string.全部),
                getString(R.string.好评),
                getString(R.string.中评),
                getString(R.string.差评),
                getString(R.string.有图)
        )
    }
    val stateList = arrayListOf<String>("", "good", "neutral", "bad", "image")

    var goodsId = 0
    var goods: AbsGoodsModel? = null
    var state = ""

    val mDatas = arrayListOf<Goods2CommenModel>()
    val mAdapter by lazy { GoodsCommentsAdapter(mDatas) }
    val mRecyclerView by lazy { goods_comment_rv }
    val tab by lazy { tab_goodsdetail }
    val emptyView by lazy { newEmptyView(requireContext()) }


    override fun lazyFetchData() {

        getDatas(0)
    }

    /**加载评论日期*/
    fun getDatas(refresh: Int) {
        mViewModel.loadComment(goodsId, state, refresh)
    }


    override fun initView() {
        mSmartRefreshLayout = mDataBinding.autoGoodscomment
        mDataBinding.autoGoodscomment.setEnableRefresh(false)
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mAdapter
//            addItemDecoration(newDivider(mActivity))
        }

        for (index in gradleTextList.indices) {
            val newTab = tab.newTab()
            newTab.setText(gradleTextList[index])
            tab.addTab(newTab)
        }
        setCommentsCnt()
    }

    private fun setCommentsCnt() {
        if (TextUtils.isEmpty(state)) {
            tv_goodscomment_cnt.text = "(${commentsCnt})"
        }
        tv_goodscomment_grade.text = "${goods?.absGoodsGrade}%"
    }

    override fun initEvent() {
        tab.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab?) {

            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }

            override fun onTabSelected(p0: TabLayout.Tab?) {
                state = stateList[p0!!.position]
                getDatas(1)
                ll_goodsdetail_commens.visibility = (p0!!.position == 0).judge(View.VISIBLE, View.GONE)
            }

        })
    }

    override fun startObserver() {
        mViewModel.loadCommentLiveData.observe(this, Observer {
            showComment(it)
        })
    }

    var commentsCnt = 0

    /**
     * @Note   显示评论
     * @param  comments 评论数据
     */
    fun showComment(resultModel: ResultModel<ArrayList<Goods2CommenModel>>) {
        if (resultModel.page == 1) {
            mDatas.clear()
        }
        mAdapter.addData(resultModel.data!!)
        mDataBinding.autoGoodscomment.setEnableLoadMore(resultModel.page < resultModel.sumPage)
        commentsCnt = resultModel.totalData

        setCommentsCnt()

        mAdapter.setEmptyView(emptyView)
    }


    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }
}